<template>
  <div class="wrap">
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="auto"
      class="ruleForm-wrap"
    >
      <el-form-item label="开发者ID">
        <el-select v-model="ruleForm.dev_id" placeholder="请选择">
          <el-option
            v-for="item in dev_id_arr"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="应用名称" prop="app_name">
        <el-input
          v-model="ruleForm.app_name"
          message
          placeholder="英文字母开头，可包含数字、中划线、下划线"
        />
      </el-form-item>
      <el-form-item class="btn-wrap">
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          注册
        </el-button>
        <el-button type="warning" plain @click="resetForm(ruleFormRef)"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { postData } from '@/http/request.js'
import { useRouter } from 'vue-router'
let router = useRouter()
let ruleFormRef = ref(null)
let dev_id_arr = ref([])

const ruleForm = reactive({
  dev_id: '',
  app_name: '',
})

const rules = reactive({
  dev_id: [{ required: false, message: '', trigger: 'blur' }],
  app_name: [
    {
      required: true,
      message: '应用名称只能为英文开头、数字、中划线、下划线',
      trigger: 'blur',
    },
  ],
})

let get_dev_id = () => {
  postData('/dev/list', {})
    .then((res) => {
      if (res.code == 0) {
        let developers = res.datas.developers
        var res_arr = []
        for (var i = 0; i < developers.length; i++) {
          res_arr.push(developers[i].dev_id)
        }
        dev_id_arr.value = res_arr
      } else if (res.code == -56055) {
        return
      } else {
        ElMessage({
          type: 'error',
          message: 'error',
        })
      }
    })
    .catch((res) => {
      ElMessage({
        type: 'error',
        message: 'error',
      })
    })
}

const submitForm = async (formEl) => {
  console.log(ruleForm)
  console.log(formEl)
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      postData('/dev/app/register', {
        dev_id: ruleForm.dev_id,
        app_name: ruleForm.app_name
      })
        .then((res) => {
          if (res.code == 0) {
            ElMessage({
              type: 'success',
              message: '注册成功',
            })
            setTimeout(() => {
              router.push('/application')
            }, 3000)
          } else {
            ElMessage({
              type: 'error',
              message: '注册失败',
            })
          }
        })
        .catch((err) => {
          console.log(err)
        })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}

onMounted(() => {
  get_dev_id()
})
</script>
<style scope>
.wrap {
  margin: 20px 0 0 20px;
}
.btn-wrap {
  margin-top: 30px;
}
</style>
